package com.ruoyi.system.vo.cpc;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author knight.yang
 *
 */
@Controller
public class CpcUtils {

	public static List<Province> provinces = new ArrayList<>();
	static String china_province_city_path = "cpc_201901.json";

	public static String loadStringCityString() {
		try {
			ClassPathResource resource = new ClassPathResource(china_province_city_path);
			File root_file = resource.getFile();
			String root_json = FileUtils.readFileToString(root_file, "utf-8");
			return root_json;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	static {
		loadDate();
	}

	/**
	 * 根据城市代号，获取城市名字
	 * 
	 * @param code
	 * @return
	 */
	public static String findProvinceCityName(String code) {
		for (Province bean : provinces) {
			List<City> cityList = bean.getCityList();
			for (City city : cityList) {
				if (city.getCode().equals(code)) {
					return bean.getName() + "-" + city.getName();
				}
			}
		}
		return null;
	}

	// public static List<FrontCityBean> loadDate(String pid) {
	// List<FrontCityBean> leve0 = new ArrayList<>();
	// FrontCityBean front;
	// if ("0".equals(pid)) {
	// for (Province bean : provinces) {
	// front = new FrontCityBean();
	// front.setAreaCode(bean.getCode());
	// front.setChinaRegionId(bean.getCode());
	// front.setDivisionCode(bean.getCode());
	// front.setDivisionName(bean.getName());
	// front.setId(bean.getCode());
	// front.setLatitude("");
	// front.setLongitude("");
	// front.setRanking(bean.getCode());
	// front.setParentId("0");
	// leve0.add(front);
	// }
	// } else if (pid.endsWith("000")) {
	// for (Province beans : provinces) {
	// if (beans.getCode().equals(pid)) {
	// for (City bean : beans.getCityList()) {
	// front = new FrontCityBean();
	// front.setAreaCode(bean.getCode());
	// front.setChinaRegionId(bean.getCode());
	// front.setDivisionCode(bean.getCode());
	// front.setDivisionName(bean.getName());
	// front.setId(bean.getCode());
	// front.setLatitude("");
	// front.setLongitude("");
	// front.setRanking(bean.getCode());
	// front.setParentId("0");
	// leve0.add(front);
	// }
	// return leve0;
	// }
	// }
	// } else {
	// for (Province beans : provinces) {
	// for (City bean : beans.getCityList()) {
	// if (bean.getCode().equals(pid)) {
	// for (Area are : bean.getAreaList()) {
	// front = new FrontCityBean();
	// front.setAreaCode(are.getCode());
	// front.setChinaRegionId(are.getCode());
	// front.setDivisionCode(are.getCode());
	// front.setDivisionName(are.getName());
	// front.setId(are.getCode());
	// front.setLatitude("");
	// front.setLongitude("");
	// front.setRanking(are.getCode());
	// front.setParentId("0");
	// leve0.add(front);
	// }
	// return leve0;
	// }
	// }
	// }
	// }
	// return leve0;
	// }

	public static void loadDate() {
		try {
			ClassPathResource resource = new ClassPathResource(china_province_city_path);
			File root_file = resource.getFile();
			String root_json = FileUtils.readFileToString(root_file);
			JSONArray province_array = JSONArray.fromObject(root_json);
			for (int i = 0; i < province_array.size(); i++) {
				Province p = new Province();
				JSONObject P_object = province_array.getJSONObject(i);
				p.setName(P_object.get("name").toString());
				p.setCode(P_object.get("code").toString());
				JSONArray city_array = JSONArray.fromObject(P_object.get("cityList"));
				List<City> citys = new ArrayList<>();
				for (int j = 0; j < city_array.size(); j++) {
					City c = new City();
					JSONObject c_object = city_array.getJSONObject(j);
					c.setName(c_object.get("name").toString());
					c.setCode(c_object.get("code").toString());
					// 暂时关闭区域显示
					// JSONArray area_array = JSONArray.fromObject(c_object.get("areaList"));
					// List<Area> areas = new ArrayList<>();
					// for (int h = 0; h < area_array.size(); h++) {
					// Area a = new Area();
					// JSONObject a_object = area_array.getJSONObject(h);
					// a.setCode(a_object.getString("code").toString());
					// a.setName(a_object.getString("name").toString());
					// areas.add(a);
					// }
					// c.setAreaList(areas);
					citys.add(c);
				}
				p.setCityList(citys);
				provinces.add(p);
			}
		} catch (Exception e) {
		}
	}
	//
	// public static void main(String[] args) throws Exception {
	//// String s = loadStringCityString();
	//// String s = findCityName("530400");
	//// System.out.println(s);
	// java.util.Random rand = new java.util.Random();
	// int randNum = rand.nextInt(13);
	// System.out.println(randNum);
	//
	//// //使用数字专用类Random,公式new Random().nextInt()(max-min+1)+min
	//// //生成10到30之间的数字
	//// int max= 30;
	//// int min = 10;
	//// int num = new Random().nextInt(max-min+1)+min;
	//// System.out.println(num+"1");
	//// System.out.println("111111");
	//
	// }

	public static String findCodeByName(String name) {
		name = name.replaceAll(" ", "");
		for (Province bean : provinces) {
			List<City> cityList = bean.getCityList();
			for (City city : cityList) {
				if (city.getName().startsWith(name)) {
					return city.getCode();
				}
			}
		}
		for (Province bean : provinces) {
			List<City> cityList = bean.getCityList();
			for (City city : cityList) {
				if (name.startsWith(city.getName())) {
					return city.getCode();
				}
			}
		}
		return null;
	}

}
